//
//  SecondViewController.m
//  Demo CMTabBarController
//
//  Created by Constantine Mureev on 15.03.12.
//  Copyright (c) 2012 Team Force LLC. All rights reserved.
//

#import "DVActivityListVC.h"
#import "UIViewController+CMTabBarController.h"
#import "DVSetting.h"
#import "DVActivityVC.h"
#import "DVActData.h"
#import "DVCalendarVC.h"

@implementation DVActivityListVC {
    NSMutableArray *_dealedArray;
    NSMutableArray *_unDealedArray;
}

- (id)init {
    self = [super initWithStyle:UITableViewStyleGrouped];
    return self;
}

- (void)resetData {
    if (!_dealedArray) {
        _dealedArray = [NSMutableArray array];
    }
    
    if (!_unDealedArray) {
        _unDealedArray = [NSMutableArray array];
    }
    
    [_dealedArray removeAllObjects];
    [_unDealedArray removeAllObjects];
    
    for (DVActivity *tmpData in [DVActData sharedActData].actList) {
        if (tmpData.type == DVActivityTypeDealed) {
            [_dealedArray addObject:tmpData];
        } else {
            [_unDealedArray addObject:tmpData];
        }
    }
}

- (void)reloadData {
    NSMutableArray * section1 = [NSMutableArray array];
    for (DVActivity *tmpDealedData in _dealedArray) {
        DVActivityItem *tmpItem = [DVActivityItem itemWithActivity:tmpDealedData];
        [tmpItem setSelectAction:@selector(onActivity:) target:self];
        [section1 addObject:tmpItem];
    }
    
    NSMutableArray * section2 = [NSMutableArray array];
    for (DVActivity *tmpUndealedData in _unDealedArray) {
        DVActivityItem *tmpItem = [DVActivityItem itemWithActivity:tmpUndealedData];
        [tmpItem setSelectAction:@selector(onActivity:) target:self];
        [section2 addObject:tmpItem];
    }
    
    _items = [NSMutableArray array];
    [_items addObject:section1];
    [_items addObject:section2];
    
    self.sections = [NSArray array];
    self.items = _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.title = @"活动列表";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"日历" style:UIBarButtonItemStylePlain target:self action:@selector(onCanlender)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(onAddActivity)];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self resetData];
    [self reloadData];
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 40;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"待处理邀请";
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = [UIColor yellowColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        return label;
    }
    
    return nil;
}

#pragma mark -

- (void)onActivity :(DVActivityItem*)tmpItem {
    DVActivityVC *vc = [[DVActivityVC alloc] initWithActivity:tmpItem.activity mode:ViewModeNone];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onCanlender {
    DVCalendarVC *vc = [[DVCalendarVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onAddActivity {
    DVActivityVC *vc = [[DVActivityVC alloc] initWithActivity:nil mode:ViewModeCreate];
    [self.navigationController pushViewController:vc animated:YES];
}

@end


#pragma mark -

@implementation DVActivityItem

+ (id)itemWithActivity:(DVActivity*)tmpActivity {
    DVActivityItem *item = [[self alloc] initWithIcon:nil
                                                title:[tmpActivity.orderedTime description]
                                               detail:tmpActivity.title
                                           accessIcon:nil];
    item.activity = tmpActivity;
    return item;
}

- (int)cellHeightWithWidth:(int)width {
    return 40;
}

@end

@implementation DVActivityCell

@end
